# class base for controller
from google.appengine.ext import webapp

# needed for display and change positions
from tools import wiki_manager, position_wiki_manager

class Index(webapp.RequestHandler):
    '''
    '''

    def get(self):
        '''
        '''
        # get the language id
        locale_id = self.request.get('language')
        # get the size name
        size = self.request.get('size')

        #
        wi_manager = wiki_manager.WikiManager(
                locale_id=locale_id, size=size)

        # display the wiki to show
        template = wi_manager.show()
        return self.response.out.write(template)

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        size_key = self.request.get('size_key')

        #
        wi_manager = wiki_manager.WikiManager(
                language_key=language_key, size_key=size_key)

        wiki_key = self.request.get('wiki_key')

        # retrieve the title from posting
        title = self.request.get('title')
        if not title == '':
            # find the corresponding key
            wiki_key = wi_manager.get_wiki_key(title)

        # display the page to show
        template = wi_manager.show(wiki_key)
        return self.response.out.write(template)

class Edit(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        size_key = self.request.get('size_key')

        #
        wi_manager = wiki_manager.WikiManager(
                language_key=language_key, size_key=size_key)

        wiki_key = None

        if not self.request.get('new') == '':
            #
            wiki_key = wi_manager.new_wiki()

        if not self.request.get('edit') == '':
            # get title
            wiki_key = self.request.get('wiki_key')

        # retrieve the title from posting
        title = self.request.get('title')
        if not title == '':
            # find the corresponding key
            wiki_key = wi_manager.get_wiki_key(title)

        # display the page to edit
        template = wi_manager.edit(wiki_key)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        size_key = self.request.get('size_key')

        # retrieve the page title of the category
        wiki_key = self.request.get('wiki_key')

        #
        wi_manager = wiki_manager.WikiManager(
                language_key=language_key, size_key=size_key)

        # delete
        if not self.request.get('delete') == '':
            # tools delete the page
            wi_manager.delete(wiki_key)

            # go to the index page
            return self.redirect('/wiki?language=%s&size=%s' % (
                wi_manager.language.locale_id, wi_manager.size.width))

        elif not self.request.get('new') == '':
            #
            wiki_key = wi_manager.new_wiki()

        # cancel
        elif not self.request.get('cancel') == '':
            # display the page to show
            template = wi_manager.show(wiki_key)
            return self.response.out.write(template)

        # we save and display the page
        else:
            # get content
            language = self.request.get('language')

            # get title
            title = self.request.get('title')

            # get content
            content = self.request.get('content')

            # tools save the page
            wi_manager.save(wiki_key, title, content)

        # stay in the edit part
        template = wi_manager.edit(wiki_key)
        return self.response.out.write(template)

class Position(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the key to edit
        language_key = self.request.get('language_key')
        # get the key to edit
        size_key = self.request.get('size_key')
        # get title
        wiki_key = self.request.get('wiki_key')

        up = not self.request.get('up') == ''

        #
        po_manager = position_wiki_manager.PositionManager()

        #
        po_manager.position(wiki_key, up)

        #
        wi_manager = wiki_manager.WikiManager(
                language_key=language_key, size_key=size_key)

        # display the page to edit
        template = wi_manager.edit(wiki_key)
        return self.response.out.write(template)
